package com.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.domain.StCourseChapters;
import com.st.mapper.StCourseChaptersMapper;
import com.st.po.CourseChaptersPo;
import com.st.service.IStCourseChaptersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 课程章节Service业务层处理
 *
 * @author zhw
 * @date 2024-10-24
 */
@Service
public class StCourseChaptersServiceImpl extends ServiceImpl<StCourseChaptersMapper, StCourseChapters> implements IStCourseChaptersService {
    @Autowired
    private StCourseChaptersMapper stCourseChaptersMapper;

    /**
     * 查询课程章节
     *
     * @param id 课程章节主键
     * @return 课程章节
     */
    @Override
    public StCourseChapters selectStCourseChaptersById(Integer id) {
        return stCourseChaptersMapper.selectStCourseChaptersById(id);
    }

    /**
     * 查询课程章节列表
     *
     * @param stCourseChapters 课程章节
     * @return 课程章节
     */
    @Override
    public List<StCourseChapters> selectStCourseChaptersList(StCourseChapters stCourseChapters) {
        return stCourseChaptersMapper.selectStCourseChaptersList(stCourseChapters);
    }

    /**
     * 新增课程章节
     *
     * @param stCourseChapters 课程章节
     * @return 结果
     */
    @Override
    public int insertStCourseChapters(StCourseChapters stCourseChapters) {
            return stCourseChaptersMapper.insertStCourseChapters(stCourseChapters);
    }

    /**
     * 修改课程章节
     *
     * @param stCourseChapters 课程章节
     * @return 结果
     */
    @Override
    public int updateStCourseChapters(StCourseChapters stCourseChapters) {
        return stCourseChaptersMapper.updateStCourseChapters(stCourseChapters);
    }

    /**
     * 批量删除课程章节
     *
     * @param ids 需要删除的课程章节主键
     * @return 结果
     */
    @Override
    public int deleteStCourseChaptersByIds(Integer[] ids) {
        return stCourseChaptersMapper.deleteStCourseChaptersByIds(ids);
    }

    /**
     * 删除课程章节信息
     *
     * @param id 课程章节主键
     * @return 结果
     */
    @Override
    public int deleteStCourseChaptersById(Integer id) {
        return stCourseChaptersMapper.deleteStCourseChaptersById(id);
    }

    @Override
    public int insertStCourseChapters1(CourseChaptersPo courseChaptersPo) {
        Integer courseId = courseChaptersPo.getCourseId();
        List<HashMap<String, String>> courseChapterInfoList = courseChaptersPo.getCourseChapterInfoList();
        courseChapterInfoList.stream().forEach(ci -> {
            String chapterName = ci.get("chapterName");
            String videoUrl = ci.get("videoUrl");
            int times = Integer.parseInt(ci.get("times"));
            StCourseChapters stCourseChapters = new StCourseChapters();
            stCourseChapters.setCourseId(courseId);
            stCourseChapters.setChapterName(chapterName);
            stCourseChapters.setVideoUrl(videoUrl);
            stCourseChapters.setDurationMinutes(times);
            stCourseChaptersMapper.insertStCourseChapters(stCourseChapters);
        });
        return 1;
    }
}